﻿@{
    ViewBag.Title = "mpedit";
}
@section scripts
{
    <link href="~/Scripts/miniui/themes/default/large-mode.css" rel="stylesheet" type="text/css" />       
    <script src="@Url.Content("~/Scripts/shared/share.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/shared/shown.js")" type="text/javascript"></script>
}
<form id="form1">

            <div class="mini-toolbar" style="padding:2px;border-top:0;border-left:0;border-right:0;"> 
                <table style="width:100%;">
                    <tr>
                        <td style="width:100%;">
                            <a class="mini-button" iconCls="icon-close" onclick="CloseWindow" plain="true">关闭</a>
                        </td>
                    </tr>
                </table> 
            </div> 
    
        <table style="width:660px;margin-top:15px;">
            <tr>
                <td bgcolor="#D7FFFF" style="font-size:12pt;">会员信息：</td>
            </tr>
        </table>
        <table  style="width:660px;">
                <tr style="height:35px;">
                    <td width ="80px" align="right" style="font-size:12pt;">会员卡号： </td>
                    <td width ="190px">
                        <input id="FNumber" class="mini-buttonedit"style="width:188px; top: 0px; left: 0px;" 
                            onbuttonclick="onButtonEdit" name="FNumber" onvaluechanged ="GetMemberID" textName="b" align="left"/>
                     </td>
                    <td align="right" style="font-size:12pt;">会员姓名：</td>
                    <td>
                       <input name="FName" class="mini-textbox" style="width:188px;" readonly="readonly"/></td>
                    <td align="center">      
                        <a class="mini-button" onclick="GetMemberID" style="width:70px;margin-right:10px;">读卡</a>             
                    </td>
                </tr>

                <tr style="height:35px;">
                    <td width ="80px" align="right" style="font-size:12pt;">
                        会员分类：</td>
                    <td width ="190px">
                        <input id="className" name="className" class="mini-textbox" enabled="false" style="width:188px;" />
                    </td>
                    <td  align="right" style="font-size:12pt;">
                        当前余额：</td>
                    <td >
                        <input name="FCurrentAmount" class="mini-spinner" minValue="-999999" maxValue="99999999" 
                            changeOnMousewheel ="false" showbutton="false" increment ="0" showbutton="false" enabled="false" style="width:188px;" required="true"/></td>
                </tr>
                <tr style="height:35px;">
                    <td align="right" style="font-size:12pt;">当前积分：</td>
                    <td >
                        <input name="FPoint" class="mini-spinner" minValue="-9999999" maxValue="99999999" 
                            changeOnMousewheel ="false" showbutton="false" increment ="0" showbutton="false" enabled="false" style="width:188px;" required="true"/></td>
                    <td align="right" style="font-size:12pt;">折扣：</td>
                    <td >
                       <input name="FDiscount" class="mini-spinner" minValue="0" maxValue="99999999" enabled="false" changeOnMousewheel ="false" increment ="0" showbutton="false" style="width:188px;"/>
                    </td>
                </tr>
                <tr style="height:35px;">
                    <td align="right" style="font-size:12pt;">联系电话：</td>
                    <td >
                        <input name="FPhone" class="mini-textbox" style="width:188px;" enabled="false"  maxlength="50" />
                    <td align="right" style="font-size:12pt;">联系地址：</td>
                    <td >
                        <input name="FAddress" class="mini-textbox" style="width:188px;" enabled="false"  maxlength="100" />
                    </td>
                </tr>
                <tr style="height:35px;">
                    <td align="right" style="font-size:12pt;">备注：</td>
                    <td colspan="3" >
                        <input name="FRemark" class="mini-textbox" style="width:470px;"  maxlength="100"  enabled="false"/>
                    </td>
                    <td></td>
                </tr>
        </table>

            <table style="width:660px;">
                <tr>
                    <td bgcolor="#D7FFFF"><span id="titles" style="font-size:12pt;">会员增加积分：</span></td>
                </tr>
            </table>

            <table style="width:660px;">
                <tr style="height:35px;">
                    <td width="80px" align="right" style="font-size:12pt;">加积分：</td>
                    <td >
                        <input name="FAmount" class="mini-spinner" value="0" minValue="-1000000" maxValue="1000000"
                        changeOnMousewheel ="false" showbutton="false" style="width:470px;" selectOnFocus="true"/>
                    </td>
                    <td align="center">      
                        <a class="mini-button" name="AddPoint" onclick="AddPoint" style="width:70px;margin-right:10px;">加积分</a>             
                    </td>
                </tr>
                <tr style="height:35px;">
                    <td align="right" style="font-size:12pt;">备注：</td>
                    <td >
                        <input name="FaddRemark" class="mini-textbox" style="width:470px;"  maxlength="100" />
                    </td>
                    <td></td>
                </tr>
            </table>

            <table style="width:660px;margin-top:15px;">
                <tr>
                    <td bgcolor="#FFD7D7"><span id="titles" style="font-size:12pt;">会员减少积分：</span></td>
                </tr>
            </table>

            <table style="width:660px;">
                <tr style="height:35px;">
                    <td width="80px" align="right" style="color:#ff6a00;font-size:12pt;">减积分：</td>
                    <td >
                        <input name="FPayPoint" class="mini-spinner" value="0" minValue="0" maxValue="1000000"
                        changeOnMousewheel ="false" showbutton="false" style="width:470px;" selectOnFocus="true"/>
                    </td>
                    <td align="center">      
                        <a class="mini-button" name="SubPoint" onclick="SubPoint" style="width:70px;margin-right:10px;">减积分</a>             
                    </td>
                </tr>
                <tr style="height:35px;">
                    <td align="right" style="font-size:12pt;">备注：</td>
                    <td>
                        <input name="FsubRemark" class="mini-textbox" style="width:470px;"  maxlength="100" />
                    </td>
                    <td></td>
                </tr>
            </table>

           
    <input name="FItemID" class="mini-hidden" />
    <input name="Type" value="@ViewBag.Type" class="mini-hidden" />
    <input name="BillNo" value="@ViewBag.BillNo" class="mini-hidden" />
    <input name="BillID" value="@ViewBag.BillID" class="mini-hidden" />
    <input name="FSetAmount" value="0" class="mini-hidden" />
    </form>
<script>
    function GetQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]); return null;
    }

    $(function () {
        GetBillNo();
    });

    function GetMemberID() {
        var FNumber = mini.getbyName("FNumber").getText();
        var ID = mini.getbyName("FNumber").getValue();
        if (FNumber == ID) ID = "0";
        $.post("GetMembers", { json: "[{'FItemID':'" + ID + "','FNumber':'" + FNumber + "', 'mSystemAppID':'" + parent.window.getSystemAppID() + "'}]" }, function (data) {
            var mdata = mini.decode(data);
            if (mdata.data.length > 0) {
                mini.getbyName("FNumber").setValue(mdata.data[0].FItemID);
                mini.getbyName("className").setValue(mdata.data[0].className);
                mini.getbyName("FName").setValue(mdata.data[0].FName);
                mini.getbyName("FRemark").setValue(mdata.data[0].FRemark);
                mini.getbyName("FCurrentAmount").setValue(mdata.data[0].FAmount);
                mini.getbyName("FPoint").setValue(mdata.data[0].FPoint);
                mini.getbyName("FPhone").setValue(mdata.data[0].FPhone);
                mini.getbyName("FAddress").setValue(mdata.data[0].FAddress);
                mini.getbyName("FDiscount").setValue(mdata.data[0].FDiscount);
                if (mdata.data[0].FPointFlag) 
                    isEnabled(true);
                else
                    isEnabled(false);
            }
        });
    }

    function isEnabled(Enabled) {
        mini.getbyName("FAmount").setEnabled(Enabled); 
        mini.getbyName("FSetAmount").setEnabled(Enabled);
        mini.getbyName("AddPoint").setEnabled(Enabled);
        mini.getbyName("FaddRemark").setEnabled(Enabled);
        mini.getbyName("FPayPoint").setEnabled(Enabled);
        mini.getbyName("SubPoint").setEnabled(Enabled);
        mini.getbyName("FsubRemark").setEnabled(Enabled);
    }

    function NewInfo() {
        mini.getbyName("FNumber").setText('');
        mini.getbyName("FNumber").setValue('');
        mini.getbyName("className").setValue('');
        mini.getbyName("FName").setValue('');
        mini.getbyName("FRemark").setValue('');
        mini.getbyName("FAmount").setValue(0);
        mini.getbyName("FSetAmount").setValue(0);
        mini.getbyName("FCurrentAmount").setValue(0);
        mini.getbyName("FPoint").setValue(0);
        mini.getbyName("FPayPoint").setValue(0);
        mini.getbyName("FPhone").setValue('');
        mini.getbyName("FAddress").setValue('');
        mini.getbyName("FDiscount").setValue(1);
        mini.getbyName("FsubRemark").setValue('');
        mini.getbyName("FaddRemark").setValue('');
    }

    function onButtonEdit() {
        mini.open({
            url: "../GetItem/index",
            title: "会员选择", width: 730, height: 460,
            onload: function () {
                var iframe = this.getIFrameEl();
                var data = { action: "new", sTbName: "Member", sSearchKey: "" };
                iframe.contentWindow.SetData(data);
            },
            ondestroy: function (action) {
                if (action.length > 0) {
                    mini.getbyName("FNumber").setText(action[0].FNumber);
                    mini.getbyName("FNumber").setValue(action[0].FItemID);
                    mini.getbyName("FName").setValue(action[0].FName);
                    GetMemberID(action.FNumber);
                }
            }
        });
    }

    function AddPoint() {
        if (mini.getbyName("FNumber").getValue() == '') {
            mini.alert("未选择会员");
            return;
        }
        mini.getbyName("AddPoint").setEnabled(false);
        var form = new mini.Form("#form1");
        form.validate();
        if (form.isValid() == false) return;
        var data = form.getData();
        if (data.FAmount < 0 || data.FSetAmount < 0) {
            mini.showTips({ content: '充值积分不能为负数', state: "danger", x: "center", y: "top", timeout: 3000 });
            mini.getbyName("AddPoint").setEnabled(true);
            return;
        }
        data.ptype = 2;
        var json = mini.encode(data);
        $.post("savePoint", { json: json, mSystemAppID: parent.window.getSystemAppID() }, function (data) {
            var ret = mini.decode(data);
            if (ret.status == 1) {
                mini.showTips({ content: ret.msg, state: "success", x: "center", y: "top", timeout: 3000 });
                GetBillNo();
                NewInfo();
            }
            else {
                mini.alert("异常:" + ret.error);
            }
            mini.getbyName("AddPoint").setEnabled(true);
        });
    }

    function SubPoint() {
        if (mini.getbyName("FNumber").getValue() == '') {
            mini.alert("未选择会员");
            return;
        }
        mini.getbyName("SubPoint").setEnabled(false);
        var form = new mini.Form("#form1");
        form.validate();
        if (form.isValid() == false) return;
        var data = form.getData();
        if (data.FPayPoint < 0 ) {
            mini.alert('减积分数不能是负数');
            mini.getbyName("SubPoint").setEnabled(true);
            return;
        }
        if (data.FPoint < data.FPayPoint) {
            mini.alert('减积分数不能大于当前积分');
            mini.getbyName("SubPoint").setEnabled(true);
            return;
        }
        data.FAmount = 0 - data.FPayPoint;
        data.FSetAmount = 0;
        data.ptype = -2;
        var json = mini.encode(data);
        $.post("savePoint", { json: json, mSystemAppID: parent.window.getSystemAppID() }, function (data) {
            var ret = mini.decode(data);
            if (ret.status == 1) {
                mini.showTips({ content: ret.msg, state: "success", x: "center", y: "top", timeout: 3000 });
                GetBillNo();
                NewInfo();
            }
            else {
                mini.alert("异常:" + ret.error);
            }
            mini.getbyName("SubPoint").setEnabled(true);
        });
    }


    function GetBillNo() {
        var Json = '[{"type":"' + mini.getbyName("Type").getValue() + '","mSystemAppID":"' + parent.window.getSystemAppID() + '"}]';
        $.post("GetMemberPayBillNo", { json: Json }, function (data) {
            var mdata = mini.decode(data);
            if (mdata.BillOrder.length > 0) {
                mini.getbyName("BillNo").setValue(mdata.BillOrder[0].FBillNo);
                mini.getbyName("BillID").setValue(mdata.BillOrder[0].FEntryID);
            }
        });
    }

    function FormShown() { mini.parse();}
</script>

